//
//  ChooseJourneyViewController.m
//  TravelFollowYourHeart
//
//  Created by ENERGY on 2018/1/23.
//  Copyright © 2018年 Lwx. All rights reserved.
//
#define topBGViewH 40
#define bottomLineViewX 10

#import "ChooseJourneyViewController.h"
#import "MyCollectionViewCell.h"
#import "MyCollectionModel.h"
#import "TravelInforViewController.h"
#import "ActivityDetailViewController.h"
#import "TravelsViewController.h"
#import "TravelDateViewController.h"
#import "XXTripTableViewController.h"
@interface ChooseJourneyViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong)UICollectionView * myCollectionView;
@property (nonatomic, strong)UIView * topBGView;
@property (nonatomic, strong)UIView * bottomLineView;
@property (nonatomic, strong)MyCollectionModel * model;
@property (nonatomic, strong)NSString * tidStr;
@property (nonatomic, assign)int rowNum;
@property (nonatomic, strong)NSString * cityStr;
@property (nonatomic, strong)UIViewController * lastVC;
@property (nonatomic, strong)NSMutableArray * dataArra;
@property (nonatomic, assign)int tid;//旅游信息ID
@end

@implementation ChooseJourneyViewController(Networking)
- (void)getCollectionListWithType:(int)type{
    NSDictionary * dic = @{
                           @"rule":@(1),
                           @"page":@(self.rowNum),
                           @"row":@(6),
                           @"lng":@([LocationTool shareIntance].longitude),
                           @"lat":@([LocationTool shareIntance].latitude),
                           @"city":self.city
                           };
    WS(ws)
    [[HttpRequest shardWebUtil] postNetworkRequestURLString:Url(@"/index/travels") andParas:dic andTransferGottenData:^(id obj, NSError *error) {
        [self.myCollectionView.mj_header endRefreshing];
        [self.myCollectionView.mj_footer endRefreshing];
        if (error)
        {
            [[HUDHelper sharedInstance]tipMessage:obj[@"msg"]];
        }
        else
        {
            if ([obj[@"code"] integerValue] == 100000)
            {
                ws.model = [MyCollectionModel yy_modelWithDictionary:obj];
                if (self.rowNum == 1) {
                    [self.dataArra removeAllObjects];
                }
                [self.dataArra addObjectsFromArray:ws.model.data];
                [self.myCollectionView reloadData];
                
            }
            else
            {
                [[HUDHelper sharedInstance]tipMessage:obj[@"msg"]];
            }
        }
    }];
}
//一天加入行程
- (void)addTripBtnClick{
    NSDictionary * dic = @{
                           @"mid":[UserModel intance].member_id,
                           //                           @"token":[UserModel intance].token,
                           @"trip_id":@([self.trip_id intValue]),
                           @"day":@(self.travelDay),//因为是新建行程，所以只有第一天行程，所以只传1
                           //                           @"day":@(1),
                           @"tid":@([self.tidStr intValue])
                           };
    
    WS(ws)
    [[HttpRequest shardWebUtil] postNetworkRequestURLString:Url(@"/index/addOneday") andParas:dic andTransferGottenData:^(id obj, NSError *error) {
        if (error) {
            [[HUDHelper sharedInstance] tipMessage:obj[@"msg"]];
            
        }else{
            if ([obj[@"code"] integerValue] == 100000){

            }else{
                [[HUDHelper sharedInstance] tipMessage:obj[@"msg"]];
            }
        }
    }];
}
@end
@implementation ChooseJourneyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tidStr = nil;
    self.view.backgroundColor = getColor(bgColor);
    [self.view addSubview:self.myCollectionView];
    self.title = @"选择行程";
    
    self.rowNum = 1;
    self.dataArra = [NSMutableArray array];
    NSArray *vcsArray = [self.navigationController viewControllers];
    NSInteger vcCount = vcsArray.count;
    _lastVC = vcsArray[vcCount-2];
    
//    self.selectTravelDays = self.travelDay;
    if ([_lastVC isKindOfClass:[TravelDateViewController class]]) {
        self.travelDay = 1;
    }else if ([_lastVC isKindOfClass:[XXTripTableViewController class]]){
        self.city = @"";
    }
    [self getCollectionListWithType:self.rowNum];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (UICollectionView *)myCollectionView{
    if (!_myCollectionView) {
        UICollectionViewFlowLayout * myCollectionViewFlowLayout = [[UICollectionViewFlowLayout alloc] init];
        _myCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) collectionViewLayout:myCollectionViewFlowLayout];
        _myCollectionView.backgroundColor = getColor(bgColor);
        _myCollectionView.delegate = self;
        _myCollectionView.dataSource = self;
        [_myCollectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:@"myCollectionCell"];
        
        myCollectionViewFlowLayout.itemSize = CGSizeMake(SCREEN_WIDTH / 2 - 2, 200);
        myCollectionViewFlowLayout.sectionInset = UIEdgeInsetsZero;
        myCollectionViewFlowLayout.minimumInteritemSpacing = 0;
        myCollectionViewFlowLayout.minimumLineSpacing = 5;
        myCollectionViewFlowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        
        self.myCollectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            self.rowNum = 1;
            [self getCollectionListWithType:self.rowNum];
        }];
        self.myCollectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            self.rowNum ++;
            [self getCollectionListWithType:self.rowNum];
        }];
        [self.myCollectionView.mj_header beginRefreshing];
    }return _myCollectionView;
}

#pragma collectionDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArra.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MyCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"myCollectionCell" forIndexPath:indexPath];
    cell.modelData = self.dataArra[indexPath.row];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"myCollectionCell";
    MyCollectionViewCell * cell = (MyCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
    cell.modelData = self.dataArra[indexPath.row];
    _tidStr = cell.modelData.collectId;
    NSLog(@"%@", _tidStr);
    
    TravelInforViewController * travelInforVC = [[TravelInforViewController alloc] init];
    travelInforVC.trip_id = self.trip_id;
    travelInforVC.tid = [_tidStr intValue];
    travelInforVC.travelDays = self.travelDay;
//    [[NSUserDefaults standardUserDefaults] setObject:@(self.selectTravelDays) forKey:@"selectTravelDays"];
//    [[NSUserDefaults standardUserDefaults] synchronize];
    travelInforVC.selectTravelDays = self.selectTravelDays;
//    [self addTripBtnClick];
    [self.navigationController pushViewController:travelInforVC animated:YES];
}

@end
